package com.ruoyi.common.rabbit.conditional;

/**
 * @Description: TODO
 * @Author: j.zh
 * @Date: 2024/3/12
 * @Version: 1.0
 **/

import com.ruoyi.common.rabbit.configuration.RabbitMqProperties;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * 判断是否在启用了Rabbit, 用来控制在没启用Rabbit情况下. 不将 @RabbitListener 修饰的监听器注册为Bean, 不然会导致无限尝试重连
 * @Author: j.zh
 * @Date: 2024/3/12
 * @Version: 1.0
 **/
public class OnRabbitEnable implements Condition {

    private final String rabbitPropertiesPrefix = "com.ruoyi.common.rabbit";

    /**
     * @param context
     * @param metadata
     * @return
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        RabbitMqProperties rabbitMqProperties = Binder.get(context.getEnvironment())
                .bind(rabbitPropertiesPrefix, RabbitMqProperties.class)
                .orElse(new RabbitMqProperties());
        return rabbitMqProperties.isEnable();
    }

}

